package com.buaa.student.service.impl;


import com.buaa.student.mapper.DakaMapper;
import com.buaa.student.mapper.StudentMapper;
import com.buaa.student.pojo.Student;
import com.buaa.student.service.DakaService;
import com.buaa.student.vo.DNDakaStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Service
public class DakaServiceImpl implements DakaService {

    @Autowired
    private DakaMapper dakaMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public int addRecord(String xingming, String xuehao, Date date) {
        //每次插入记录后都要去重
        int record = dakaMapper.addRecord(xingming, xuehao, date);

        return record;
    }

    @Override
    public ArrayList<DNDakaStudent> getNewWeiDakaList(Date date, int minTime, int maxTime) {

        /**
         * 0.查询之前先去重
         */
        int mum = dakaMapper.elimintaeDup();

        /**
         * 1.先查出今日未打卡的同学名单
         */
        ArrayList<Student> todayList = dakaMapper.getTodayWeiDaka(date);

        /**
         * 2.再查出打卡次数=time次的同学名单
         */
        ArrayList<Student> threeTimesList = dakaMapper.getNTimes(minTime, maxTime);

        /**
         * 3.循环遍历，在今日的未打卡同学名单中找出等于time次的那类
         */
        ArrayList<DNDakaStudent> targetList = new ArrayList<>();
        for (Student todayStudent : todayList) {
            for (Student threeTimesStudent : threeTimesList) {
                if(todayStudent.getXingming().equals(threeTimesStudent.getXingming())){

                    DNDakaStudent dnDakaStudent = new DNDakaStudent();

                    dnDakaStudent.setXingming(todayStudent.getXingming());
                    dnDakaStudent.setXuehao(todayStudent.getXuehao());

                    ArrayList<Student> students = studentMapper.queryStudentByKey(todayStudent.getXingming());
                    dnDakaStudent.setBanji(students.get(0).getBanji());

                    targetList.add(dnDakaStudent);
                    break;
                }
            }
        }

        /**
         * 4.补全今日未打卡且次数=time次的同学的历史记录
         */
        for (DNDakaStudent dnDakaStudent : targetList) {
            ArrayList<Date> historyList = dakaMapper.queryHistoryByXingming(dnDakaStudent.getXingming());
            dnDakaStudent.setHistory(historyList);
        }

        return targetList;
    }
}
